package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.HouseBrokerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController {
    @DubboReference
    private HouseBrokerService houseBrokerService;

    /**
     *
     * @param houseId 房源id
     * @param map 请求域
     * @return 到create页面
     */
    @RequestMapping("/create")
    public String create(Long houseId, Map map){
        //查找当前房源经纪人外的其他经纪人id
        List<Admin> adminList = houseBrokerService.findOtherBrokerByHouseId(houseId);
        //将当前房源外的其他房源经纪人放到请求域
        map.put("adminList",adminList);
        //将houseId放到请求域
        map.put("houseId",houseId);
        return "houseBroker/create";
    }

    /**
     * 新增
     * @param houseBroker 房源经纪人
     * @return 成功页面
     */
    @RequestMapping("/save")
    public String save(HouseBroker houseBroker){
        houseBrokerService.insert(houseBroker);
        return "common/successPage";
    }

    /**
     *
     * @param houseId 房源id
     * @param houseBrokerId 房源经纪人id
     * @return 重定向到房源首页,顺便展示房源id
     */
    @RequestMapping("/delete/{houseId}/{houseBrokerId}")
    public String delete(@PathVariable Long houseId,@PathVariable Long houseBrokerId){
        houseBrokerService.delete(houseBrokerId);
        return "redirect:/house/show/" + houseId;
    }
}
